﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;
using System.Data;

namespace CRM.Web.PotentialCustomerCaseManage
{
    public partial class List : Common.PageBase
    {
        public string ModifyUrl = "Modify.aspx?CASEID={0}";
        T_POTENTIALCUSTOMERCASE_BLL bll = new T_POTENTIALCUSTOMERCASE_BLL();
        /// <summary>
        /// 工单状态业务访问对象
        /// </summary>
        private T_WORKORDERSTATUS_BLL workOrderStatusBll = new T_WORKORDERSTATUS_BLL();
        /// <summary>
        /// 角色状态业务访问对象
        /// </summary>
        private T_ROLEORDERSTATUSAUTHORITY_BLL roleOrderStatusAuthorityBll = new T_ROLEORDERSTATUSAUTHORITY_BLL();
        /// <summary>
        /// 数据字典业务访问对象
        /// </summary>
        private T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            //PageHelper.SetPopCalender(this.Page, txtTODOREMINDERTIMEBegin);
            //PageHelper.SetPopCalender(this.Page, txtTODOREMINDERTIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtCREATETIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEEnd);
            //PageHelper.SetPopCalender(this.Page, txtFOLLOWUPTIMEBegin);
            //PageHelper.SetPopCalender(this.Page, txtFOLLOWUPTIMEEnd);
            PageHelper.SetPopCalender(this.Page, txtVISITTIMEBegin);
            PageHelper.SetPopCalender(this.Page, txtVISITTIMEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                lblExportMaxRows.Text = configBll.GetCacheValueByName(this.Page, "ExportMaxRows");
                txtRecordBegin.Text = StaticConstant.RecordBegin;
                txtRecordEnd.Text = StaticConstant.RecordEnd;

                ddlSTATUSBinding();

                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定

        private void ddlSTATUSBinding()
        {
            List<T_WORKORDERSTATUS_Entity> list = workOrderStatusBll.GetSessionList(this.Page,
                                                                                    ((int)WorkOrderType.潜在客户).ToString());

            ddlSTATUS.DataSource = list;
            ddlSTATUS.DataTextField = "OrderStatusName";
            ddlSTATUS.DataValueField = "OrderStatusID";
            ddlSTATUS.DataBind();
            ddlSTATUS.Items.Insert(0, new ListItem("", ""));

        }



        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["CASEID"]))
            {
                sql.Append(" and CASEID like '%" + Request.QueryString["CASEID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CALLER"]))
            {
                sql.Append(" and CALLER like '%" + Request.QueryString["CALLER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CALLED"]))
            {
                sql.Append(" and CALLED like '%" + Request.QueryString["CALLED"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CALLID"]))
            {
                sql.Append(" and CALLID = '" + Request.QueryString["CALLID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RECORDCODE"]))
            {
                sql.Append(" and RECORDCODE like '%" + Request.QueryString["RECORDCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CALLTYPE"]))
            {
                sql.Append(" and CALLTYPE like '%" + Request.QueryString["CALLTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]))
            {
                sql.Append(" and CUSTOMERID = '" + Request.QueryString["CUSTOMERID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERCODE"]))
            {
                sql.Append(" and CUSTOMERCODE like '%" + Request.QueryString["CUSTOMERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERNAME"]))
            {
                sql.Append(" and CUSTOMERNAME like '%" + Request.QueryString["CUSTOMERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTCONTACTTEL"]))
            {
                sql.Append(" and FIRSTCONTACTTEL like '%" + Request.QueryString["FIRSTCONTACTTEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ADDRESS"]))
            {
                sql.Append(" and ADDRESS like '%" + Request.QueryString["ADDRESS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COUNTRY"]))
            {
                sql.Append(" and COUNTRY like '%" + Request.QueryString["COUNTRY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PROVINCE"]))
            {
                sql.Append(" and PROVINCE like '%" + Request.QueryString["PROVINCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PREFECTURECITY"]))
            {
                sql.Append(" and PREFECTURECITY like '%" + Request.QueryString["PREFECTURECITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CITY"]))
            {
                sql.Append(" and CITY like '%" + Request.QueryString["CITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGETOWN"]))
            {
                sql.Append(" and VILLAGETOWN like '%" + Request.QueryString["VILLAGETOWN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGE"]))
            {
                sql.Append(" and VILLAGE like '%" + Request.QueryString["VILLAGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GROUPTEAM"]))
            {
                sql.Append(" and GROUPTEAM like '%" + Request.QueryString["GROUPTEAM"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CONSULTINGTYPE"]))
            {
                sql.Append(" and CONSULTINGTYPE like '%" + Request.QueryString["CONSULTINGTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VINCODE"]))
            {
                sql.Append(" and VINCODE like '%" + Request.QueryString["VINCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BRAND"]))
            {
                sql.Append(" and BRAND like '%" + Request.QueryString["BRAND"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLETYPE"]))
            {
                sql.Append(" and VEHICLETYPE like '%" + Request.QueryString["VEHICLETYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PRODUCTCODE"]))
            {
                sql.Append(" and PRODUCTCODE like '%" + Request.QueryString["PRODUCTCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SERIES"]))
            {
                sql.Append(" and SERIES like '%" + Request.QueryString["SERIES"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MODELS"]))
            {
                sql.Append(" and MODELS like '%" + Request.QueryString["MODELS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMODEL"]))
            {
                sql.Append(" and ENGINEMODEL like '%" + Request.QueryString["ENGINEMODEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINEMANUFACTURERS"]))
            {
                sql.Append(" and ENGINEMANUFACTURERS like '%" + Request.QueryString["ENGINEMANUFACTURERS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ENGINENUMBER"]))
            {
                sql.Append(" and ENGINENUMBER like '%" + Request.QueryString["ENGINENUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CONSULTINGCONTENT"]))
            {
                sql.Append(" and CONSULTINGCONTENT like '%" + Request.QueryString["CONSULTINGCONTENT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["HANDLING"]))
            {
                sql.Append(" and HANDLING like '%" + Request.QueryString["HANDLING"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["RESULTS"]))
            {
                sql.Append(" and RESULTS like '%" + Request.QueryString["RESULTS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["TODOREMINDERTIMEBegin"]))
            {
                sql.Append(" and TODOREMINDERTIME >= '" + Request.QueryString["TODOREMINDERTIMEBegin"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["TODOREMINDERTIMEEnd"]))
            {
                sql.Append(" and TODOREMINDERTIME < '" + Request.QueryString["TODOREMINDERTIMEEnd"].Trim() + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMINDERTIME"]))
            {
                sql.Append(" and REMINDERTIME like '%" + Request.QueryString["REMINDERTIME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMINDERNUMBER"]))
            {
                sql.Append(" and REMINDERNUMBER like '%" + Request.QueryString["REMINDERNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                //sql.Append(" and CREATETIME >= '" + Request.QueryString["CREATETIMEBegin"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                //sql.Append(" and CREATETIME < '" + Request.QueryString["CREATETIMEEnd"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                //sql.Append(" and LASTMODIFYTIME >= '" + Request.QueryString["LASTMODIFYTIMEBegin"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                //sql.Append(" and LASTMODIFYTIME < '" + Request.QueryString["LASTMODIFYTIMEEnd"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONID"]))
            {
                sql.Append(" and BUSINESSDIVISIONID = '" + Request.QueryString["BUSINESSDIVISIONID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONNAME"]))
            {
                sql.Append(" and BUSINESSDIVISIONNAME like '%" + Request.QueryString["BUSINESSDIVISIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BUSINESSDIVISIONCODE"]))
            {
                sql.Append(" and BUSINESSDIVISIONCODE like '%" + Request.QueryString["BUSINESSDIVISIONCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERCODE"]))
            {
                sql.Append(" and DEALERCODE like '%" + Request.QueryString["DEALERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERNAME"]))
            {
                sql.Append(" and DEALERNAME like '%" + Request.QueryString["DEALERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATIONCODE"]))
            {
                sql.Append(" and STATIONCODE like '%" + Request.QueryString["STATIONCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATIONNAME"]))
            {
                sql.Append(" and STATIONNAME like '%" + Request.QueryString["STATIONNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSMOBILEPHONE"]))
            {
                sql.Append(" and OWNERSMOBILEPHONE like '%" + Request.QueryString["OWNERSMOBILEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSOFFICEPHONE"]))
            {
                sql.Append(" and OWNERSOFFICEPHONE like '%" + Request.QueryString["OWNERSOFFICEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSHOMEPHONE"]))
            {
                sql.Append(" and OWNERSHOMEPHONE like '%" + Request.QueryString["OWNERSHOMEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSPEOPLE"]))
            {
                sql.Append(" and OWNERSPEOPLE like '%" + Request.QueryString["OWNERSPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["OWNERSPEOPLECODE"]))
            {
                sql.Append(" and OWNERSPEOPLECODE like '%" + Request.QueryString["OWNERSPEOPLECODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERBASE"]))
            {
                sql.Append(" and CUSTOMERBASE like '%" + Request.QueryString["CUSTOMERBASE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERTYPE"]))
            {
                sql.Append(" and CUSTOMERTYPE like '%" + Request.QueryString["CUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERLEVEL"]))
            {
                sql.Append(" and CUSTOMERLEVEL like '%" + Request.QueryString["CUSTOMERLEVEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIGCUSTOMERTYPE"]))
            {
                sql.Append(" and BIGCUSTOMERTYPE like '%" + Request.QueryString["BIGCUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEUSERTYPE"]))
            {
                sql.Append(" and CREATEUSERTYPE like '%" + Request.QueryString["CREATEUSERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEDEPARTMENTID"]))
            {
                sql.Append(" and CREATEDEPARTMENTID = '" + Request.QueryString["CREATEDEPARTMENTID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEDEPARTMENTNAME"]))
            {
                sql.Append(" and CREATEDEPARTMENTNAME like '%" + Request.QueryString["CREATEDEPARTMENTNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLEMARKCODE"]))
            {
                sql.Append(" and VEHICLEMARKCODE like '%" + Request.QueryString["VEHICLEMARKCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VEHICLEMARKNAME"]))
            {
                sql.Append(" and VEHICLEMARKNAME like '%" + Request.QueryString["VEHICLEMARKNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FOLLOWUPTIMEBegin"]))
            {
                //sql.Append(" and FOLLOWUPTIME >= '" + Request.QueryString["FOLLOWUPTIMEBegin"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetBeginDateString("FOLLOWUPTIME", Request.QueryString["FOLLOWUPTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FOLLOWUPTIMEEnd"]))
            {
                //sql.Append(" and FOLLOWUPTIME < '" + Request.QueryString["FOLLOWUPTIMEEnd"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetEndDateString("FOLLOWUPTIME", Request.QueryString["FOLLOWUPTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FOLLOWUPCONTENT"]))
            {
                sql.Append(" and FOLLOWUPCONTENT like '%" + Request.QueryString["FOLLOWUPCONTENT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VISITTIMEBegin"]))
            {
                //sql.Append(" and VISITTIME >= '" + Request.QueryString["VISITTIMEBegin"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetBeginDateString("VISITTIME", Request.QueryString["VISITTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VISITTIMEEnd"]))
            {
                //sql.Append(" and VISITTIME < '" + Request.QueryString["VISITTIMEEnd"].Trim() + "'");
                sql.Append(" and " + bll._dal.GetEndDateString("VISITTIME", Request.QueryString["VISITTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VISITREMARK"]))
            {
                sql.Append(" and VISITREMARK like '%" + Request.QueryString["VISITREMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISVISITCOMPLATE"]))
            {
                sql.Append(" and ISVISITCOMPLATE like '%" + Request.QueryString["ISVISITCOMPLATE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["USERRECOMMENDED"]))
            {
                sql.Append(" and USERRECOMMENDED like '%" + Request.QueryString["USERRECOMMENDED"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISINITIATIVECONTACT"]))
            {
                sql.Append(" and ISINITIATIVECONTACT like '%" + Request.QueryString["ISINITIATIVECONTACT"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISUNDERSTANDVEHICLEINFO"]))
            {
                sql.Append(" and ISUNDERSTANDVEHICLEINFO like '%" + Request.QueryString["ISUNDERSTANDVEHICLEINFO"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISHASPURCHASE"]))
            {
                sql.Append(" and ISHASPURCHASE like '%" + Request.QueryString["ISHASPURCHASE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SOURCE"]))
            {
                sql.Append(" and SOURCE like '%" + Request.QueryString["SOURCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CAUSEANALYSIS"]))
            {
                sql.Append(" and CAUSEANALYSIS like '%" + Request.QueryString["CAUSEANALYSIS"] + "%'");
            }

            #region 权限部分
            if (!UserLoginInfo.IsAdministrator())
            {
                //操作员当前事业务数据
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");

            }

            #endregion

            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_POTENTIALCUSTOMERCASE_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "CASEID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }



        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string fileName = "RepairCase";
            fileName += _userLoginInfo.UserEntity.LOGINNAME.ToUpper();
            if (UserLoginInfo.IsAdministrator())
            {
                PageHelper.ShowMessage(this.Page, "请不要用系统管理导出数据！");
                return;
            }


            int recordBegin = StringHelper.ToInt(txtRecordBegin.Text.Trim(), 0);
            if (recordBegin != 0)
            {
                recordBegin--;
            }
            int recordEnd = StringHelper.ToInt(txtRecordEnd.Text.Trim(), 0);
            if (recordEnd != 0)
            {
                recordEnd--;
            }
            int recordCount = recordEnd - recordBegin;
            if (recordCount >= StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "ExportMaxRows"), 10000))
            {
                PageHelper.ShowMessage("导出区间大于单次导出数！");
                return;
            }

            StringBuilder sql = GenerationSQLString();
            DataTable dataTable = bll.GetDataTable(sql.ToString(), recordBegin, recordCount, gvList.SortConditions);


            //foreach(DataColumn dc in dataTable.Columns)
            //{
            //    LogHelper.WriteLog("test", String.Format("ColumnName:{0}--FullName:{1}--Name:{2}", dc.ColumnName, dc.DataType.FullName, dc.DataType.Name));
            //}


            List<NPOIExcelHelper.FieldInfo> fieldInfos = new List<NPOIExcelHelper.FieldInfo>();

            #region 导出数据列

            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CASEID", "工单编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CALLER", "来电号码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CALLED", "被叫号码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CALLID", "主叫标识"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("RECORDCODE", "录音代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CALLTYPE", "来电类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERID", "客户编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERCODE", "客户代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERNAME", "客户姓名"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FIRSTCONTACTTEL", "首选联系号码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ADDRESS", "详细地址"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("COUNTRY", "国家"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PROVINCE", "省份"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PREFECTURECITY", "地级市"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CITY", "区/县/市"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VILLAGETOWN", "乡/镇/街道"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VILLAGE", "村"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("GROUPTEAM", "组/队"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CONSULTINGTYPE", "咨询类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VINCODE", "整机编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BRAND", "品牌"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VEHICLETYPE", "类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PRODUCTCODE", "产品代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SERIES", "系列"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MODELS", "机型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINEMODEL", "发动机型号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINEMANUFACTURERS", "发动机厂家"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ENGINENUMBER", "发动机编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CONSULTINGCONTENT", "咨询内容"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATUS", "处理状态", workOrderStatusBll.GetList().ToDictionary(p => p.ORDERSTATUSID.ToString(), p => p.ORDERSTATUSNAME)));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("HANDLING", "处理方式"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("RESULTS", "处理结果"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("REMARK", "备注"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("TODOREMINDERTIME", "待办提醒时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("REMINDERTIME", "待办提醒时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("REMINDERNUMBER", "提醒次数"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATETIME", "创建时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLEID", "创建人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLE", "创建人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLEID", "最后修改人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLE", "最后修改人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYTIME", "最后修改时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONID", "事业部编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONNAME", "事业部名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONCODE", "事业部代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEALERCODE", "经销商代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEALERNAME", "经销商名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATIONCODE", "服务商代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATIONNAME", "服务商名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSMOBILEPHONE", "车主手机"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSOFFICEPHONE", "车主办公电话"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSHOMEPHONE", "车主家庭电话"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSPEOPLE", "车主姓名"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("OWNERSPEOPLECODE", "车主代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERBASE", "客户群"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERTYPE", "客户类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERLEVEL", "客户等级"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERSTATUS", "客户状态"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BIGCUSTOMERTYPE", "大客户类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEUSERTYPE", "创建操作员类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEDEPARTMENTID", "创建部门编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEDEPARTMENTNAME", "创建部门名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VEHICLEMARKCODE", "车辆市场部代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VEHICLEMARKNAME", "车辆市场部名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FOLLOWUPTIME", "跟进时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FOLLOWUPCONTENT", "跟进内容"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VISITTIME", "回访时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VISITREMARK", "回访备注"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISVISITCOMPLATE", "是否回访成功", EnumHelper.EnumToDictionary(typeof(IsVisitComplate)).ToDictionary(p => p.Key.ToString(), p => p.Value)));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("USERRECOMMENDED", "用户建议"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISINITIATIVECONTACT", "经销商是否主动联系"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISUNDERSTANDVEHICLEINFO", "是否对车辆基本信息了解"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISHASPURCHASE", "是否已购机"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SOURCE", "客户购车信息来源"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CAUSEANALYSIS", "（已购车或已决定购车、犹豫中、购买其它品牌）客户原因分析"));



            #endregion

            NPOIExcelHelper.MemoryStreamMaxRows = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "MemoryStreamMaxRows"), 0);
            fileName = NPOIExcelHelper.DataTableToExcel(dataTable, fieldInfos, fileName + ".xls");
            UserLoginInfo.WriteLog(LogType.导出, "潜在客户导出", "潜在客户管理", fileName);
            dataTable.Clear();
            dataTable = null;
            GC.Collect();
        }


        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_POTENTIALCUSTOMERCASE_Entity entity = new T_POTENTIALCUSTOMERCASE_Entity();
            entity.CASEID = StringHelper.ToLong((sender as GridView).DataKeys[e.RowIndex].Values[0].ToString());

            long rel = bll.Delete(entity);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }
                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值
                                switch (bf.DataField)
                                {
                                    case "STATUS":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                T_WORKORDERSTATUS_Entity entity =
                                                    workOrderStatusBll.GetPageItem_T_WORKORDERSTATUS_EntityByKey(this.Page, StringHelper.ToInt(temp, 0));
                                                if (entity != null)
                                                {
                                                    temp = entity.ORDERSTATUSNAME;
                                                    if (!String.IsNullOrEmpty(entity.RECORDSTYLE))
                                                    {
                                                        e.Row.Attributes["style"] = entity.RECORDSTYLE;
                                                    }
                                                }
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {

                                            }
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }
                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
